# 16_jes2001_2016_participation_in_system.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-14 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("tidyr")
require("ggplot2")
require("texreg")
quartzFonts(HiraKaku = quartzFont(rep('Hiragino Kaku Gothic Pro W3', 4)))
theme_set(theme_classic(base_size = 12, base_family = 'HiraKaku'))


# 2003
## merge
JES2003_Ideology <- JES2003_Respondent[,c("ID", "Ideology")]
JES2003_Ideology <- as.data.frame(JES2003_Ideology)
JES2003_Ideology <- left_join(JES2003_Ideology, 
                              JES2003[,c("ID", "GENDER", "EDUCAT03", "BORNY", "E11_2", "E11_3", "E11_4", "E11_5", "D8")], 
                              by = "ID")

## recode
JES2003_Ideology$System <- JES2003_Ideology$E11_2 + JES2003_Ideology$E11_3 + JES2003_Ideology$E11_4 + JES2003_Ideology$E11_5
JES2003_Ideology$Ideology_strong <- abs(JES2003_Ideology$Ideology - 
                                          median(JES2003_Ideology$Ideology, na.rm = TRUE))
JES2003_Ideology$D8[JES2003_Ideology$D8 %in% c(88, 99)] <- NA
JES2003_Ideology$Support <- ifelse(JES2003_Ideology$D8 == 9, 0, 1)
JES2003_Ideology$Female <- ifelse(JES2003_Ideology$GENDER == 2, 1, 0)
JES2003_Ideology$EDUCAT03[JES2003_Ideology$EDUCAT03 == 9] <- NA
JES2003_Ideology$College <- ifelse(JES2003_Ideology$EDUCAT03 == 4, 1, 0)
JES2003_Ideology$Age <- 2003 - JES2003_Ideology$BORNY

## regression
glm2003 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = JES2003_Ideology, family = "poisson")
summary(glm2003)


# 2005
## merge
JES2005_Ideology <- JES2005_Respondent[,c("ID", "Ideology")]
JES2005_Ideology <- as.data.frame(JES2005_Ideology)
JES2005_Ideology <- left_join(JES2005_Ideology, 
                              JES2005[,c("ID", "GENDER", "EDUCAT05", "BORNY", "K21_2", "K21_3", "K21_4", "K21_5", "J10")], 
                              by = "ID")

## recode
JES2005_Ideology$System <- JES2005_Ideology$K21_2 + JES2005_Ideology$K21_3 + JES2005_Ideology$K21_4 + JES2005_Ideology$K21_5
JES2005_Ideology$Ideology_strong <- abs(JES2005_Ideology$Ideology - 
                                          median(JES2005_Ideology$Ideology, na.rm = TRUE))
JES2005_Ideology$J10[JES2005_Ideology$J10 %in% c(88, 99)] <- NA
JES2005_Ideology$Support <- ifelse(JES2005_Ideology$J10 == 9, 0, 1)
JES2005_Ideology$Female <- ifelse(JES2005_Ideology$GENDER == 2, 1, 0)
JES2005_Ideology$EDUCAT05[JES2005_Ideology$EDUCAT05 == 9] <- NA
JES2005_Ideology$College <- ifelse(JES2005_Ideology$EDUCAT05 == 4, 1, 0)
JES2005_Ideology$Age <- 2005 - JES2005_Ideology$BORNY

## regression
glm2005 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + Age, 
               data = JES2005_Ideology, family = "poisson")
summary(glm2005)


# 2007
## merge
JES2007_Ideology <- JES2007_Respondent[,c("ID", "Ideology")]
JES2007_Ideology <- as.data.frame(JES2007_Ideology)
JES2007_Ideology <- left_join(JES2007_Ideology, 
                              JES2007[,c("ID", "F1", "F4", "F2_AGE", "Q51x2", "Q51x3", "Q51x4", "Q51x5", "Q20")], 
                              by = "ID")

## recode
JES2007_Ideology$System <- JES2007_Ideology$Q51x2 + JES2007_Ideology$Q51x3 + JES2007_Ideology$Q51x4 + JES2007_Ideology$Q51x5
JES2007_Ideology$Ideology_strong <- abs(JES2007_Ideology$Ideology - 
                                          median(JES2007_Ideology$Ideology, na.rm = TRUE))
JES2007_Ideology$Q20[JES2007_Ideology$Q20 %in% c(88, 99)] <- NA
JES2007_Ideology$Support <- ifelse(JES2007_Ideology$Q20 == 9, 0, 1)
JES2007_Ideology$Female <- ifelse(JES2007_Ideology$F1 == 2, 1, 0)
JES2007_Ideology$F4[JES2007_Ideology$F4 == 9] <- NA
JES2007_Ideology$College <- ifelse(JES2007_Ideology$F4 == 4, 1, 0)

## regression
glm2007 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2007_Ideology, family = "poisson")
summary(glm2007)


# 2009
## merge
JES2009_Ideology <- JES2009_Respondent[,c("ID", "Ideology")]
JES2009_Ideology <- as.data.frame(JES2009_Ideology)
JES2009_Ideology <- left_join(JES2009_Ideology, 
                              JES2009[,c("ID", "F1", "F4", "F2_AGE", "Q10")], 
                              by = "ID")
JES2009_2 <- read_sav(
  "~/Dropbox/Dataset/JESIV/0999_wave3.sav", 
  user_na = FALSE
)
JES2009_Ideology <- left_join(JES2009_Ideology, 
                              JES2009_2[,c("ID", "Q23x2", "Q23x3", "Q23x4", "Q23x5")], 
                              by = "ID")

## recode
JES2009_Ideology$System <- JES2009_Ideology$Q23x2 + JES2009_Ideology$Q23x3 + JES2009_Ideology$Q23x4 + JES2009_Ideology$Q23x5
JES2009_Ideology$Ideology_strong <- abs(JES2009_Ideology$Ideology - 
                                          median(JES2009_Ideology$Ideology, na.rm = TRUE))
JES2009_Ideology$Q10[JES2009_Ideology$Q10 %in% c(88, 99)] <- NA
JES2009_Ideology$Support <- ifelse(JES2009_Ideology$Q10 == 9, 0, 1)
JES2009_Ideology$Female <- ifelse(JES2009_Ideology$F1 == 2, 1, 0)
JES2009_Ideology$F4[JES2009_Ideology$F4 == 9] <- NA
JES2009_Ideology$College <- ifelse(JES2009_Ideology$F4 == 4, 1, 0)

## regression
glm2009 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2009_Ideology, family = "poisson")
summary(glm2009)


# 2010
## merge
JES2010_Ideology <- JES2010_Respondent[,c("ID", "Ideology")]
JES2010_Ideology <- as.data.frame(JES2010_Ideology)
JES2010_Ideology <- left_join(JES2010_Ideology, 
                              JES2010[,c("ID", "F1", "F4", "F2_AGE", "Q8")], 
                              by = "ID")
JES2010_2 <- read_sav(
  "~/Dropbox/Dataset/JESIV/0999_wave6.sav", 
  user_na = FALSE
)
JES2010_Ideology <- left_join(JES2010_Ideology, 
                              JES2010_2[,c("ID", "Q29x2", "Q29x3", "Q29x4", "Q29x5")], 
                              by = "ID")

## recode
JES2010_Ideology$System <- JES2010_Ideology$Q29x2 + JES2010_Ideology$Q29x3 + JES2010_Ideology$Q29x4 + JES2010_Ideology$Q29x5
JES2010_Ideology$Ideology_strong <- abs(JES2010_Ideology$Ideology - 
                                          median(JES2010_Ideology$Ideology, na.rm = TRUE))
JES2010_Ideology$Q8[JES2010_Ideology$Q8 %in% c(88, 99)] <- NA
JES2010_Ideology$Support <- ifelse(JES2010_Ideology$Q8 == 11, 0, 1)
JES2010_Ideology$Female <- ifelse(JES2010_Ideology$F1 == 2, 1, 0)
JES2010_Ideology$F4[JES2010_Ideology$F4 == 9] <- NA
JES2010_Ideology$College <- ifelse(JES2010_Ideology$F4 == 4, 1, 0)

## regression
glm2010 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2010_Ideology, family = "poisson")
summary(glm2010)


# 2012 (自治会や町内会への参加がない点に注意)
## merge
JES2012_Ideology <- JES2012_Respondent[,c("id", "Ideology")]
JES2012_Ideology <- as.data.frame(JES2012_Ideology)
JES2012_Ideology <- left_join(JES2012_Ideology, 
                              JES2012[,c("id", "f1", "f4", "f2", "q60s2_3", "q60s2_7", "q60s2_9", "q54")], 
                              by = "id")

## recode
JES2012_Ideology$q60s2_3[is.na(JES2012_Ideology$q60s2_3)] <- 0
JES2012_Ideology$q60s2_7[is.na(JES2012_Ideology$q60s2_7)] <- 0
JES2012_Ideology$q60s2_9[is.na(JES2012_Ideology$q60s2_9)] <- 0
JES2012_Ideology$System <- JES2012_Ideology$q60s2_3 + JES2012_Ideology$q60s2_7 + JES2012_Ideology$q60s2_9
JES2012_Ideology$Ideology_strong <- abs(JES2012_Ideology$Ideology - 
                                          median(JES2012_Ideology$Ideology, na.rm = TRUE))
JES2012_Ideology$Support <- ifelse(JES2012_Ideology$q54 == 15, 0, 1)
JES2012_Ideology$Female <- ifelse(JES2012_Ideology$f1 == 2, 1, 0)
JES2012_Ideology$f4[JES2012_Ideology$f4 == 9] <- NA
JES2012_Ideology$College <- ifelse(JES2012_Ideology$f4 == 4, 1, 0)

## regression
glm2012 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + f2, 
               data = JES2012_Ideology, family = "poisson")
summary(glm2012)


# 2013
## merge
JES2013_Ideology <- JES2013_Respondent[,c("id", "Ideology")]
JES2013_Ideology <- as.data.frame(JES2013_Ideology)
JES2013_Ideology <- left_join(JES2013_Ideology, 
                              JES2013[,c("id", "af1", "af4", "af2_age", "b25_2", "b25_3", "b25_4", "b25_5", "a12x1")], 
                              by = "id")

## recode
JES2013_Ideology$System <- JES2013_Ideology$b25_2 + JES2013_Ideology$b25_3 + JES2013_Ideology$b25_4 + JES2013_Ideology$b25_5
JES2013_Ideology$Ideology_strong <- abs(JES2013_Ideology$Ideology - 
                                          median(JES2013_Ideology$Ideology, na.rm = TRUE))
JES2013_Ideology$Support <- ifelse(JES2013_Ideology$a12x1 == 8, 0, 1)
JES2013_Ideology$Female <- ifelse(JES2013_Ideology$af1 == 2, 1, 0)
JES2013_Ideology$af4[JES2013_Ideology$af4 == 9] <- NA
JES2013_Ideology$College <- ifelse(JES2013_Ideology$af4 == 4, 1, 0)

## regression
glm2013 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + af2_age, 
               data = JES2013_Ideology, family = "poisson")
summary(glm2013)


# 2014
## merge
JES2014_Ideology <- JES2014_Respondent[,c("id", "Ideology")]
JES2014_Ideology <- as.data.frame(JES2014_Ideology)
JES2014_Ideology <- left_join(JES2014_Ideology, 
                              JES2014[,c("id", "sex", "a55", "age", "b26_2", "b26_3", "b26_4", "b26_5", "a7")], 
                              by = "id")

## recode
JES2014_Ideology$System <- JES2014_Ideology$b26_2 + JES2014_Ideology$b26_3 + JES2014_Ideology$b26_4 + JES2014_Ideology$b26_5
JES2014_Ideology$Ideology_strong <- abs(JES2014_Ideology$Ideology - 
                                          median(JES2014_Ideology$Ideology, na.rm = TRUE))
JES2014_Ideology$Support <- ifelse(JES2014_Ideology$a7 == 9, 0, 1)
JES2014_Ideology$Female <- ifelse(JES2014_Ideology$sex == 2, 1, 0)
JES2014_Ideology$a55[JES2014_Ideology$a55 == 9] <- NA
JES2014_Ideology$College <- ifelse(JES2014_Ideology$a55 == 4, 1, 0)

## regression
glm2014 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + age, 
               data = JES2014_Ideology, family = "poisson")
summary(glm2014)


# 2016
## merge
JES2016_Ideology <- JES2016_Respondent[,c("id", "Ideology")]
JES2016_Ideology <- as.data.frame(JES2016_Ideology)
JES2016_Ideology <- left_join(JES2016_Ideology, 
                              JES2016[,c("id", "b_F1_SEX", "F4", "b_F2_AGE", "b_Q12_2", "b_Q12_3", "b_Q12_4", "b_Q12_5", "Q5")], 
                              by = "id")

## recode
JES2016_Ideology$System <- JES2016_Ideology$b_Q12_2 + JES2016_Ideology$b_Q12_3 + JES2016_Ideology$b_Q12_4 + JES2016_Ideology$b_Q12_5
JES2016_Ideology$Ideology_strong <- abs(JES2016_Ideology$Ideology - 
                                          median(JES2016_Ideology$Ideology, na.rm = TRUE))
JES2016_Ideology$Support <- ifelse(JES2016_Ideology$Q5 == 7, 0, 1)
JES2016_Ideology$Female <- ifelse(JES2016_Ideology$b_F1_SEX == 2, 1, 0)
JES2016_Ideology$F4[JES2016_Ideology$F4 == 9] <- NA
JES2016_Ideology$College <- ifelse(JES2016_Ideology$F4 == 4, 1, 0)

## regression
glm2016 <- glm(System ~ Ideology + Ideology_strong + Support + Female + College + b_F2_AGE, 
               data = JES2016_Ideology, family = "poisson")
summary(glm2016)


# plot
glmAll <- data.frame(
  Year = rep(c(2003, 2005, 2007, 2009, 2010, 2012, 2013, 2014, 2016), each = 7), 
  Variable = rep(c("Intercept", "Ideology", "Ideological Extremism", 
                   "Partisanship", "Female", "College Education", "Age"), times = 9), 
  Estimate = c(coef(glm2003), coef(glm2005), coef(glm2007), 
               coef(glm2009), coef(glm2010), coef(glm2012), 
               coef(glm2013), coef(glm2014), coef(glm2016)), 
  SE = c(summary(glm2003)$coefficient[,2], summary(glm2005)$coefficient[,2], 
         summary(glm2007)$coefficient[,2], summary(glm2009)$coefficient[,2], 
         summary(glm2010)$coefficient[,2], summary(glm2012)$coefficient[,2], 
         summary(glm2013)$coefficient[,2], summary(glm2014)$coefficient[,2], 
         summary(glm2016)$coefficient[,2])
)
glmAll$Variable <- factor(glmAll$Variable, 
                          levels = c("Intercept", "Ideology", "Ideological Extremism", 
                                     "Partisanship", "Female", "College Education", "Age"))
glmAll_plot <- ggplot(glmAll[glmAll$Variable %in% c("Ideology", "Ideological Extremism"),], 
                      aes(x = as.factor(Year), y = Estimate, 
                          ymin = Estimate - 1.96*SE, 
                          ymax = Estimate + 1.96*SE)) + 
  geom_hline(yintercept = 0, linetype = "dashed",colour = "gray") + 
  geom_pointrange() + facet_wrap(~Variable, nrow = 8, scales = "free") + 
  labs(x = "Year", y = "Poisson Regression Coefficients", 
       title = "System-Affirming Activities (JESIII / IV / V)") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(glmAll_plot)
